//
//  ShopDetailsHeaderView.m
//  Findif
//
//  Created by Cicel Chen on 2018/10/22.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "ShopDetailsHeaderView.h"
#import "ZYBannerView.h"
#import "XHStarRateView.h"
#import <CoreLocation/CoreLocation.h>

@interface ShopDetailsHeaderView ()<ZYBannerViewDataSource, ZYBannerViewDelegate>

@property (nonatomic, strong) ZYBannerView *banner;
@property (nonatomic, strong)NSArray * dataArray;
@property (nonatomic, weak)UILabel * shopLabel;
@property (nonatomic, weak)UILabel * distanceLabel;
@property (nonatomic, weak)XHStarRateView * starView;
@property (nonatomic, weak)UILabel * commentLabel;
@property (nonatomic, strong)UILabel * timeLabel;
@property (nonatomic, strong)UILabel * localLabel;
@property (nonatomic, strong)UIImageView * timeImageView;
@property (nonatomic, strong)UIImageView * localImageView;
@property (nonatomic, strong)UIButton * callButton;
@property (nonatomic, strong)UIButton * msgButton;
@property (nonatomic, strong)UIView * oneLineView;
@property (nonatomic, strong)UIView * twoLineView;
@property (nonatomic, strong)UIImageView * couponImageView;
@property (nonatomic, strong)UIImageView * purchaseImageView;
@property (nonatomic, strong)UILabel * pointsLabel;
@property (nonatomic, strong)UILabel * purchaseLabel;
@property (nonatomic, weak)UILabel * redLabel;
@property (nonatomic, weak)UIButton * detailsButton;

@end

@implementation ShopDetailsHeaderView


- (void)setModel:(ShopListModel *)model{
    
    _model = model;
    
    self.shopLabel.text = model.merchants_name;
    int count = [model.merchants_comments_star_level intValue];
    if (count == 0) {
        
        self.starView.width = 1 * SCREENH6;
    }else{
        
        self.starView.currentRating = count;
    }
    self.commentLabel.text = [NSString stringWithFormat:@"%@条评论",model.merchants_comments_count];
//    NSString * merchants_open_time = model.merchants_open_time;
    if (model.merchants_open_time.length > 0) {
       
        self.timeLabel.text = model.merchants_open_time;
    }else{
        
        self.timeLabel.text = @"暂无营业时间";
    }
    
    self.localLabel.text = model.merchants_address;
    NSArray *array = [model.merchants_img componentsSeparatedByString:@","];
    self.dataArray = array;
    
    double lat = [model.merchants_latitude doubleValue];
    double lon = [model.merchants_longitude doubleValue];
    
    double userLat = [DEF_GET_OBJECT(@"lat") doubleValue];
    double userLon = [DEF_GET_OBJECT(@"lon") doubleValue];
    
    double res = [self distanceBetweenOrderBy:lat :userLat :lon :userLon];
    if (res > 1000) {
        
        self.distanceLabel.text = [NSString stringWithFormat:@"%.2fkm",res / 1000];
    }else{
        
        self.distanceLabel.text = [NSString stringWithFormat:@"%.2fm",res];
    }
}


- (void)setDict:(NSDictionary *)dict{
    
    _dict = dict;
    
    self.shopLabel.text = dict[@"merchants_name"];
    int count = [dict[@"merchants_comments_star_level"] intValue];
    if (count == 0) {
        
        self.starView.width = 1 * SCREENH6;
    }else{
        
        self.starView.currentRating = count;
    }
    
    self.commentLabel.text = [NSString stringWithFormat:@"%@条评论",dict[@"merchants_comments_count"]];
    self.timeLabel.text = dict[@"merchants_open_time"];
    self.localLabel.text = dict[@"merchants_address"];
    NSArray *array = [dict[@"merchants_img"] componentsSeparatedByString:@","];
    self.dataArray = array;
    
    double lat = [dict[@"merchants_latitude"] doubleValue];
    double lon = [dict[@"merchants_longitude"] doubleValue];
    
    double userLat = [DEF_GET_OBJECT(@"lat") doubleValue];
    double userLon = [DEF_GET_OBJECT(@"lon") doubleValue];
    
    double res = [self distanceBetweenOrderBy:lat :userLat :lon :userLon];
    if (res > 1000) {
        
       self.distanceLabel.text = [NSString stringWithFormat:@"%.2fkm",res / 1000];
    }else{
        
        self.distanceLabel.text = [NSString stringWithFormat:@"%.2fm",res];
    }
    
}

- (void)setDataArray:(NSArray *)dataArray{
    
    _dataArray = dataArray;
    
    [self.banner reloadData];
}

- (instancetype)initWithFrame:(CGRect)frame{
    
    if ([super initWithFrame:frame]) {
        
        [self setupUI];
    }
    
    return self;
}


- (void)setupUI{
    
    self.backgroundColor = ZRWhiteColor;
    // 配置banner
    [self setupBanner];
    
    UILabel * shopLabel = [UILabel new];
    
    shopLabel.textColor = ZRBlackColor;
    shopLabel.text = @"王者餐厅";
    shopLabel.font = [UIFont boldSystemFontOfSize:20 * SCREENH6];
    
    self.shopLabel = shopLabel;
    
    [self addSubview:self.shopLabel];
    
    UILabel * distanceLabel = [UILabel new];
    
    distanceLabel.text = @"170m";
    distanceLabel.textColor = [UIColor lightGrayColor];
    distanceLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    self.distanceLabel = distanceLabel;
    
    [self addSubview:self.distanceLabel];
    
    
    XHStarRateView *starRateView = [[XHStarRateView alloc] initWithFrame:CGRectMake(15 * SCREENH6, 278 * SCREENH6, 70 * SCREENH6, 10 * SCREENH6)];
    starRateView.isAnimation = YES; // 有动画
    starRateView.rateStyle = XHStarRateViewRateStyeFullStar; //允许不完整星评论
    //    starRateView.delegate = self;
    starRateView.currentRating = 5;
    starRateView.userInteractionEnabled = NO;
    
    self.starView = starRateView;
    [self addSubview:self.starView];
    
    
    UILabel * commentLabel = [UILabel new];
    
    commentLabel.text = @"140条评论";
    commentLabel.textColor = [UIColor lightGrayColor];
    commentLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    self.commentLabel = commentLabel;
    
    [self addSubview:self.commentLabel];
    
    
    self.timeImageView = [self setupImageViewImageWithImage:@"clock-circular-outline"];
    [self addSubview:self.timeImageView];
    self.localImageView = [self setupImageViewImageWithImage:@"place(1)"];
    [self addSubview:self.localImageView];
    self.couponImageView = [self setupImageViewImageWithImage:@"coupon(1)"];
    [self addSubview:self.couponImageView];
    self.purchaseImageView = [self setupImageViewImageWithImage:@"discount(1)"];
    [self addSubview:self.purchaseImageView];
    
    self.timeLabel = [self setupLabelWithTitle:@"12:00 AM - 21:00 PM"];
    self.timeLabel.numberOfLines = 0;
    [self addSubview:self.timeLabel];
    self.localLabel = [self setupLabelWithTitle:@"成都剑兰大道29号"];
    self.localLabel.numberOfLines = 0;
    self.localLabel.textColor = ColorWithRGB(0x4c87d8);
     UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    self.localLabel.userInteractionEnabled = YES;
    [self.localLabel addGestureRecognizer:tapGR];
    [self addSubview:self.localLabel];
    self.pointsLabel = [self setupLabelWithTitle:@"优惠券"];
    [self addSubview:self.pointsLabel];
    self.purchaseLabel = [self setupLabelWithTitle:@"在售商品"];
    [self addSubview:self.purchaseLabel];
    
    self.callButton = [self setupButtonWithImage:@"phone-receiver"];
    [self addSubview:self.callButton];
    self.msgButton = [self setupButtonWithImage:@"support"];
    [self addSubview:self.msgButton];
    
    self.oneLineView = [self setupLineView];
    [self addSubview:self.oneLineView];
    self.twoLineView = [self setupLineView];
    [self addSubview:self.twoLineView];
    
//    UILabel * redLabel = [UILabel new];
//
//    redLabel.textColor = [UIColor redColor];
//    redLabel.text = @" -$10 ";
//    redLabel.font = [UIFont systemFontOfSize:11 * SCREENH6];
//    redLabel.layer.borderWidth = 0.5;
//    redLabel.layer.borderColor = [UIColor redColor].CGColor;
//
//    self.redLabel = redLabel;
//
//    [self addSubview:self.redLabel];
    
    
    UIButton * detailsButton = [UIButton new];
    
    [detailsButton setTitle:@"详情" forState:UIControlStateNormal];
    [detailsButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [detailsButton addTarget:self action:@selector(clickDetailButton) forControlEvents:UIControlEventTouchUpInside];
    detailsButton.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    self.detailsButton = detailsButton;
    
    [self addSubview:self.detailsButton];
    
    
}

- (void)layoutSubviews{
    
    [self.shopLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.banner.mas_bottom).offset(10 * SCREENH6);
        make.width.mas_equalTo(320 * SCREENH6);
        make.leading.mas_equalTo(self).offset(15 * SCREENH6);
    }];

    [self.distanceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.shopLabel);
        make.trailing.mas_equalTo(self).offset(-15 * SCREENH6);
    }];

    [self.commentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.starView.mas_trailing).offset(5 * SCREENH6);
        make.centerY.mas_equalTo(self.starView);
    }];

    [self.msgButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(40 * SCREENH6);
        make.top.mas_equalTo(self.starView.mas_bottom).offset(5 * SCREENH6);
        make.trailing.mas_equalTo(self).offset(-15 * SCREENH6);
    }];
    
    [self.callButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.centerY.mas_equalTo(self.msgButton);
        make.trailing.mas_equalTo(self.msgButton.mas_leading).offset(-20 * SCREENH6);
    }];
    
    [self.timeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.starView);
        make.top.mas_equalTo(self.starView.mas_bottom).offset(10 * SCREENH6);
        make.width.height.mas_equalTo(14 * SCREENH6);
    }];

    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.timeImageView);
        make.leading.mas_equalTo(self.timeImageView.mas_trailing).offset(5 * SCREENH6);
        make.trailing.mas_equalTo(self.callButton.mas_leading).offset(-5 * SCREENH6);
    }];

    [self.localImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.timeImageView);
        make.top.mas_equalTo(self.timeLabel.mas_bottom).offset(5 * SCREENH6);
    }];

    [self.localLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.localImageView);
        make.trailing.mas_equalTo(self.callButton.mas_leading).offset(-5 * SCREENH6);
        make.leading.mas_equalTo(self.timeLabel);
    }];

    [self.oneLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.localImageView);
        make.trailing.mas_equalTo(self);
        make.height.mas_equalTo(1 * SCREENH6);
        make.top.mas_equalTo(self.localLabel.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.couponImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.oneLineView);
        make.top.mas_equalTo(self.oneLineView.mas_bottom).offset(15 * SCREENH6);
        make.width.mas_equalTo(25 * SCREENH6);
        make.height.mas_equalTo(15 * SCREENH6);
    }];
    
    [self.pointsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.couponImageView);
        make.leading.mas_equalTo(self.couponImageView.mas_trailing).offset(5 * SCREENH6);
    }];
    
//    [self.redLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.mas_equalTo(self.pointsLabel);
//        make.leading.mas_equalTo(self.pointsLabel.mas_trailing).offset(5 * SCREENH6);
//    }];
    
    [self.detailsButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.pointsLabel);
        make.trailing.mas_equalTo(self).offset(-15 * SCREENH6);
    }];
    
    [self.twoLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.oneLineView);
        make.top.mas_equalTo(self.couponImageView.mas_bottom).offset(15 * SCREENH6);
    }];
    
    [self.purchaseImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.twoLineView);
        make.top.mas_equalTo(self.twoLineView.mas_bottom).offset(10 * SCREENH6);
        make.width.height.mas_equalTo(14 * SCREENH6);
    }];
    
    [self.purchaseLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.purchaseImageView);
        make.leading.mas_equalTo(self.purchaseImageView.mas_trailing).offset(5 * SCREENH6);
    }];
}


- (UILabel *)setupLabelWithTitle:(NSString *)title{
    
    UILabel * label = [UILabel new];
    
    label.text = title;
    label.textColor = ZRBlackColor;
    label.font = [UIFont systemFontOfSize:15];
    
    return label;
}

- (UIImageView *)setupImageViewImageWithImage:(NSString *)image{
    
    UIImageView * imageView = [UIImageView new];
    
    imageView.image = [UIImage imageNamed:image];
    
    return imageView;
}


- (UIButton *)setupButtonWithImage:(NSString *)image{
    
    UIButton * button = [UIButton new];
    
    [button addTarget:self action:@selector(cilckCallButtonOrMesButton:) forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundColor:ZRBlackColor];
    [button setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    button.layer.cornerRadius = 20 * SCREENH6;
    button.layer.masksToBounds = YES;
    
    return button;
}

- (UIView *)setupLineView{
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xDBE1E7);
    
    return lineView;
}

- (void)setupBanner
{
    // 初始化
    self.banner = [[ZYBannerView alloc] init];
    self.banner.dataSource = self;
    self.banner.delegate = self;
    self.banner.shouldLoop = YES;
    self.banner.autoScroll = YES;
//    self.banner.pageControlFrame = CGRectMake(200, 195, 45, 32);
    
    [self addSubview:self.banner];
    
    // 设置frame
    self.banner.frame = CGRectMake(0,0,kScreenW,240 * SCREENH6);
}

#pragma mark - ZYBannerViewDataSource
// 返回 Banner 需要显示 Item(View) 的个数
- (NSInteger)numberOfItemsInBanner:(ZYBannerView *)banner
{
    return self.dataArray.count;
}

// 返回 Banner 在不同的 index 所要显示的 View (可以是完全自定义的v iew, 且无需设置 frame)
- (UIView *)banner:(ZYBannerView *)banner viewForItemAtIndex:(NSInteger)index
{
    
    NSString * imageName = self.dataArray[index];
    NSString * usrlStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,imageName];
    NSURL * url = [NSURL URLWithString:usrlStr];
    
    
    // 创建将要显示控件
    UIImageView *imageView = [[UIImageView alloc] init];
    //    imageView.image = [UIImage imageNamed:imageName];
    [imageView sd_setImageWithURL:url];
    //    [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"轮播1"]];
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    
    return imageView;
}

-(double)distanceBetweenOrderBy:(double) lat1 :(double) lat2 :(double) lng1 :(double) lng2{
    
    CLLocation *curLocation = [[CLLocation alloc] initWithLatitude:lat1 longitude:lng1];
    
    CLLocation *otherLocation = [[CLLocation alloc] initWithLatitude:lat2 longitude:lng2];
    
    double  distance  = [curLocation distanceFromLocation:otherLocation];
    
    return  distance;
    
}

#pragma mark - ZYBannerViewDelegate
// 在这里实现点击事件的处理
- (void)banner:(ZYBannerView *)banner didSelectItemAtIndex:(NSInteger)index
{
    //    NSLog(@"点击了第%ld个项目", (long)index);
}

- (void)banner:(ZYBannerView *)banner didScrollToItemAtIndex:(NSInteger)index
{
    //    NSLog(@"滚动到第%ld个项目", (long)index);
}

- (void)cilckCallButtonOrMesButton:(UIButton *)button{
    
    if (button == self.callButton) {
        
        NSMutableString* str=[[NSMutableString alloc] initWithFormat:@"tel:%@",self.model.merchants_phone];
        
        UIWebView * callWebview = [[UIWebView alloc] init];
        
        [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
        
        [self addSubview:callWebview];
    }else{
        
        if (self.shopDetailsHeaderViewDelegate && [self.shopDetailsHeaderViewDelegate respondsToSelector:@selector(pushToChat)])
        {
            [self.shopDetailsHeaderViewDelegate pushToChat];
        }
    }
}

- (void)clickDetailButton{
    
    if (self.shopDetailsHeaderViewDelegate && [self.shopDetailsHeaderViewDelegate respondsToSelector:@selector(clickCouponButton)])
    {
        [self.shopDetailsHeaderViewDelegate clickCouponButton];
    }
}

- (void)tapAction{
    
    if (self.shopDetailsHeaderViewDelegate && [self.shopDetailsHeaderViewDelegate respondsToSelector:@selector(pushToMapViewConteoller)])
    {
        [self.shopDetailsHeaderViewDelegate pushToMapViewConteoller];
    }
}

@end
